<?php
namespace Home\Controller;
use Common\Controller\CommonController;

/**
 * 集采商品
 * @author 张亚伟 <zhangyw@zhijin101.com>
 */
class JcGoodsController extends CommonController{

    public function index(){        
        $wd = I('wd');
        $map = [];
        if($wd){
            $map['goods_name'] = ['like', $wd];
        }
        list($list, $page) = $this->lists('JcGoods', $map, 'sort desc,id asc');
        $this->assign('page',$page);
        $this->assign('list', $list);
        $this->display();
    }

    public function add(){
        if(IS_POST){
            $data = I('post.');
            if(empty($data['category_id'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请选择分类'));
            }
            if(empty($data['goods_name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入标题'));
            }
            if(empty($data['img'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传图片'));
            }
            if(empty($data['imgs'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传组图'));
            }
            $data['create_time'] = time();
            $data['update_time'] = time();
            $data['content'] = html_entity_decode($data['content']);
            $model = M('JcGoods');
            $id = $model->add($data);

            if($id){
                $this->ajaxReturn(array('s'=>true,'m'=>'添加成功', 'url'=>U('index')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'添加失败!'));
            }
        }else{
            $category = M('jc_category')->where(['id'=>['neq', 4], 'status'=>1])->select();
            $category = toList($category);
            $this->assign('category', $category);
            $this->display();
        }
    }

    public function edit(){
        $id = I('id');
        if(IS_POST){
            $data = I('post.');
            if(empty($data['category_id'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请选择分类'));
            }
            if(empty($data['goods_name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入标题'));
            }
            if(empty($data['img'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传图片'));
            }
            if(empty($data['imgs'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请上传组图'));
            }

            $model = M('JcGoods');
            $data['content'] = html_entity_decode($data['content']);
            $data['update_time'] = time();
            $res = $model->where(['id'=>$id])->save($data);
            if($res!==false){
                $this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('index')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
            }
        }else{
            $info = M('JcGoods')->where(['id'=>$id])->find();
            $category = M('jc_category')->where(['id'=>['neq', 4], 'status'=>1])->select();
            $category = toList($category);
            $this->assign('info', $info);
            $this->assign('category', $category);
            $this->display();
        }
    }

    public function del(){
        $id = I('id');
        $res = M('JcGoods')->where(['id'=>$id])->delete();
        if($res){
            $this->ajaxReturn(array('s'=>true,'m'=>'删除成功', 'url'=>U('index')));
        }else{
            $this->ajaxReturn(array('s'=>false,'m'=>'删除失败!'));
        }
    }

    public function attr(){
        $goods_id = I('goods_id');

        $map = [];
        $map['goods_id'] = $goods_id;
        list($list, $page) = $this->lists('JcGoodsAttr', $map, 'id asc');

        $Count = M('JcGoodsAttr')->where($map)->count();
        $Page = new \Think\Pager($Count,10);
        $show = $Page->showhtml();

        $this->assign('page', $show);
        $this->assign('goods_id',$goods_id);
        $this->assign('list', $list);
        $this->display();
    }

    public function attr_add(){
        $goods_id = I('goods_id');

        if(IS_POST){
            $data = I('post.');
            if(empty($data['name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入名称'));
            }
            $model = M('JcGoodsAttr');
            $id = $model->add($data);
            if($id){
                $this->ajaxReturn(array('s'=>true,'m'=>'添加成功', 'url'=>U('attr', ['goods_id'=>$data['goods_id']])));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'添加失败!'));
            }
        }else{
            $this->assign('goods_id',$goods_id);
            $this->display();
        }
    }

    public function attr_edit(){
        
        $id = I('id');
        if(IS_POST){
            $data = I('post.');
            $model = M('JcGoodsAttr');
            if(empty($data['name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入名称'));
            }

            $res = $model->where(['id'=>$id])->save($data);
            if($res!==false){
                $this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('index', ['goods_id'=>$data['goods_id']])));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
            }
        }else{
            $info = M('JcGoodsAttr')->where(['id'=>$id])->find();
            $this->assign('info', $info);
            $this->display();
        }
    }

    public function attr_del(){
        
        $id = I('id');
        $goods_id = I('goods_id');
        $res = M('JcGoodsAttr')->where(['id'=>$id])->delete();
        if($res){
            $this->ajaxReturn(array('s'=>true,'m'=>'删除成功', 'url'=>U('attr', ['goods_id'=>$goods_id])));
        }else{
            $this->ajaxReturn(array('s'=>false,'m'=>'删除失败!'));
        }
    }
}